package com.practice.mybatis.v2.session;

import java.lang.reflect.Proxy;

import com.practice.mybatis.v2.config.Configuration;
import com.practice.mybatis.v2.config.MapperRegistory.MapperData;
import com.practice.mybatis.v2.executor.Executor;
import com.practice.mybatis.v2.mapper.MapperProxy;

public class SqlSession {
	
	private Configuration configuration;
	
	private Executor executor;

	public Configuration getConfiguration() {
		return configuration;
	}

	public SqlSession(Configuration configuration, Executor executor) {
		this.configuration = configuration;
		this.executor = executor;
	}
	
	@SuppressWarnings("unchecked")
	public <T> T getMapper(Class<T> clazz) {
		return (T) Proxy.newProxyInstance(clazz.getClassLoader(), 
				new Class[]{clazz}, new MapperProxy<T>(this, clazz));
	}
	
	public <T> T selectOne(MapperData<T> mapperData, String parameter) {
		return executor.query(mapperData, parameter);
	}

}
